package handler

import (
	"context"
	"fmt"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
	"sale_system/goods_srv/global"
	"sale_system/goods_srv/model"
	"sale_system/goods_srv/proto"
)

// CategoryBrandList 实现品牌分类，可以分类完成后，获得对应的品牌 获取所有的！！！
func (s *GoodsServer) CategoryBrandList(ctx context.Context, request *proto.CategoryBrandFilterRequest) (*proto.CategoryBrandListResponse, error) {

	rsp := proto.CategoryBrandListResponse{}

	var categoryBrands []model.GoodsCategoryBrand

	// 获得总数
	var total int64
	global.DB.Model(&model.GoodsCategoryBrand{}).Count(&total)
	rsp.Total = int32(total)

	// 什么时候需要预加载
	result := global.DB.Preload("Category").Preload("Brands").Scopes(Paginate(int(request.Pages), int(request.PagePerNums))).Find(&categoryBrands)
	if result.Error != nil {

		return &rsp, result.Error
	}
	// 读取 categoryBrand中的数据
	for _, categoryBrand := range categoryBrands {
		fmt.Println(categoryBrand.Brands)
		categoryBrandInfo := &proto.CategoryBrandResponse{}

		categoryBrandInfo.Category = &proto.CategoryInfoResponse{
			Id:             categoryBrand.CategoryID,
			Name:           categoryBrand.Category.Name,
			Level:          categoryBrand.Category.Level,
			IsTab:          categoryBrand.Category.IsTab,
			ParentCategory: categoryBrand.Category.ParentCategoryID,
		}
		categoryBrandInfo.Brand = &proto.BrandInfoResponse{
			Id:   categoryBrand.Brands.ID,
			Name: categoryBrand.Brands.Name,
			Logo: categoryBrand.Brands.Logo,
		}

		rsp.Data = append(rsp.Data, categoryBrandInfo)
	}

	return &rsp, nil
}

// 通过category获取brands接口

// GetCategoryBrandList 确定商品分类后，找到单个品类，获取对应的品牌列表
func (s *GoodsServer) GetCategoryBrandList(ctx context.Context, request *proto.CategoryInfoRequest) (*proto.BrandListResponse, error) {
	// 本质就是在web层获取到商品品类，即获得到品类ID，然后通过CategoryBrand找到品牌列表
	rsp := proto.BrandListResponse{}

	var category model.Category
	result := global.DB.Find(&category, request.Id).First(&category)
	if result.RowsAffected == 0 {
		return &rsp, status.Error(codes.NotFound, "当前品类不存在")
	}

	var categoryBrands []model.GoodsCategoryBrand
	result = global.DB.Preload("Brands").Where(&model.GoodsCategoryBrand{CategoryID: category.ID}).Find(&categoryBrands)
	if result.RowsAffected == 0 {
		return &rsp, status.Error(codes.InvalidArgument, "查询品牌失败")
	}

	for _, categoryBrand := range categoryBrands {

		brandInfo := &proto.BrandInfoResponse{
			Id:   categoryBrand.Brands.ID,
			Name: categoryBrand.Brands.Name,
			Logo: categoryBrand.Brands.Logo,
		}

		rsp.Data = append(rsp.Data, brandInfo)
	}

	return &rsp, nil
}

// CreateCategoryBrand 给一个确定的品类，添加一个品牌，实现两者之间的关系
func (s *GoodsServer) CreateCategoryBrand(ctx context.Context, request *proto.CategoryBrandRequest) (*proto.CategoryBrandResponse, error) {

	result := global.DB.First(&model.Category{}, request.CategoryId)
	if result.RowsAffected == 0 {
		return nil, status.Error(codes.NotFound, "商品品类不存在")
	}

	result = global.DB.First(&model.Brands{}, request.BrandId)
	if result.RowsAffected == 0 {
		return nil, status.Error(codes.NotFound, "商品品牌不存在")
	}

	categoryBrand := model.GoodsCategoryBrand{
		CategoryID: request.CategoryId,
		BrandsID:   request.BrandId,
	}

	global.DB.Save(&categoryBrand)

	return &proto.CategoryBrandResponse{Id: categoryBrand.ID}, nil

}
func (s *GoodsServer) DeleteCategoryBrand(ctx context.Context, request *proto.CategoryBrandRequest) (*emptypb.Empty, error) {

	result := global.DB.First(&model.GoodsCategoryBrand{}, request.Id)
	if result.RowsAffected == 0 {
		return &emptypb.Empty{}, status.Error(codes.NotFound, "品类、品牌关系不存在")
	}

	// 删除
	result = global.DB.Delete(&model.GoodsCategoryBrand{}, request.Id)
	if result.RowsAffected == 0 {
		return &emptypb.Empty{}, status.Error(codes.InvalidArgument, "删除失败")
	}
	return &emptypb.Empty{}, nil
}
func (s *GoodsServer) UpdateCategoryBrand(ctx context.Context, request *proto.CategoryBrandRequest) (*emptypb.Empty, error) {

	var categoryBrand model.GoodsCategoryBrand

	result := global.DB.First(&categoryBrand, request.Id)
	if result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "品牌分类不存在")
	}

	result = global.DB.First(&model.Category{}, request.CategoryId)
	if result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "商品分类不存在")
	}

	result = global.DB.First(&model.Brands{}, request.BrandId)
	if result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "品牌不存在")
	}

	categoryBrand.CategoryID = request.CategoryId
	categoryBrand.BrandsID = request.BrandId

	global.DB.Save(&categoryBrand)

	return &emptypb.Empty{}, nil
}
